package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.AdminLogService;
import com.cskaoyan.market.db.domain.MarketLog;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/log")
public class AdminLogController{
    // 获取service层对象
    @Autowired
    AdminLogService adminLogService;

    /**
     * 管理员日志列表
     * @param request 请求
     * @param response 响应
     */
    @GetMapping("/list")
    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        String key = request.getParameter("key");
        String name = request.getParameter("name");
        // TODO 完善性判定

        // 管理员日志列表的核心代码
        List<MarketLog> marketLogList = adminLogService.list(page, limit, sort, order);
        // TODO 结果判定

        // 返回响应
        Object ok = ResponseUtil.okList(marketLogList);
        response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

}